﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using nplJobsCommon.Job;
using System.Data.SqlClient;

namespace nplJobsDAL.Select
{
    public class SelectJobLocationCities : DataAccessBase
    {
        public long JobID { get; set; }

        public SelectJobLocationCities()
        {
            StoredProcedureName = StoredProcedure.Name.spGetCitiesByJob.ToString();
        }

        #region GetJobCities
        public List<JobCities> GetJobCities()
        {
            DataBaseHelper dbHelper;
            DataSet dsJobCities;

            try
            {
                dbHelper = new DataBaseHelper(StoredProcedureName);
                dbHelper.Parameters = new SelectJobCitiesParameters(JobID).Parameters;
                dsJobCities = dbHelper.Run(base.ConnectionString, dbHelper.Parameters);
                return GetList(dsJobCities);
            }
            catch (Exception)
            {

                throw;
            }
        }

        public List<JobCities> GetJobOutsideCities()
        {
            DataBaseHelper dbHelper;
            DataSet dsJobOutsideCities;

            try
            {
                dbHelper = new DataBaseHelper(StoredProcedure.Name.spGetOutsideCitiesByJob.ToString());
                dbHelper.Parameters = new SelectJobCitiesParameters(JobID).Parameters;
                dsJobOutsideCities = dbHelper.Run(base.ConnectionString, dbHelper.Parameters);
                return GetOutsideList(dsJobOutsideCities);
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion

        #region GetList
        private List<JobCities> GetList(DataSet dsJobCities)
        {
            List<JobCities> jobCities;
            try
            {
                jobCities = (from d in dsJobCities.Tables[0].AsEnumerable()
                                 select new JobCities
                                 {
                                     ID = d.Field<int>("JobID"),
                                     DivisionID = d.Field<int>("DivisionID"),
                                     DivisionName = d.Field<string>("DivisionName"),
                                     DistrictID = d.Field<int>("DistrictID"),
                                     DistrictName = d.Field<string>("DistrictName"),
                                     CityID = d.Field<int?>("CityID"),
                                     CityName = d.Field<string>("CityName")
                                 }).ToList();

                return jobCities;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
            finally { }
        }

        private List<JobCities> GetOutsideList(DataSet dsJobOutsideCities)
        {
            List<JobCities> jobOutsideCities;
            try
            {
                jobOutsideCities = (from d in dsJobOutsideCities.Tables[0].AsEnumerable()
                             select new JobCities
                             {
                                 ID = d.Field<int>("JobID"),
                                 CountryID = d.Field<int>("CountryID"),
                                 CountryName = d.Field<string>("CountryName"),
                                 CityName = d.Field<string>("CityName")
                             }).ToList();

                return jobOutsideCities;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
            finally { }
        }
        #endregion

        #region SQL parameters for selecting job alert by id
        public class SelectJobCitiesParameters
        {
            private SqlParameter[] _parameters;

            public SelectJobCitiesParameters(long jobID)
            {
                this.JobID = jobID;
                Build();
            }

            private void Build()
            {
                SqlParameter[] parameters = 
                {
                    new SqlParameter("@JobID", this.JobID),
                };

                Parameters = parameters;
            }

            public long JobID { get; set; }

            public SqlParameter[] Parameters
            {
                get { return _parameters; }
                set { _parameters = value; }
            }
        }
        #endregion

    }
}
